package com.manage.demo.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.manage.demo.dao.model.StudentInfo;
import com.manage.demo.dao.model.ClassInfo;

@Mapper
public interface StudentMapper {
	@Select("select * from student_info where (#{grade}='' or grade=#{grade}) and (#{classname}='' or class_num=#{classname}) and (#{id}='' or stu_id=#{id}) and (#{session}='' or left(student_info.stu_id, 4)=#{session}) order by stu_id limit #{limit} offset #{offset}")
	public List<StudentInfo> getStudentListByCondition(String session, String grade, String classname, String id, int limit, int offset);
	
	@Select("select stu_id from student_info where left(student_info.stu_id, 4)=#{id}")
	public List<Integer> getStudentIdByCondition(Integer id);
	
	@Select("select * from student_info where stu_id=#{id}")
	public List<Map<String, Object>> getStudentMange(String id);
	
	@Insert({
		"<script> ",
		"insert into student_info ",
		"(stu_id,name,sex,grade,class_num,tele,ID_num,create_time,update_time) ",
		"values ",
		"(#{id},#{name},#{sex},#{ingrade},#{inclassname},#{tele},#{idcard},now(),now())",
		"</script>"
	})
	public Integer setStudentByCondition(String id, String name, String sex, String ingrade, String inclassname, String tele, String idcard);
	
	@Insert({
		"<script> ",
		"insert into user_info",
		"(user_id,password,permission,Administrator,create_time,update_time) ",
		"values ",
		"(#{id},#{id},0,0,now(),now())",
		"</script>"
	})
	public Integer setUserByCondition(String id);
	
	@Update("update student_info set name=#{name}, sex=#{sex}, grade=#{ingrade}, class_num=#{inclassname}, tele=#{tele}, ID_num=#{idcard}, update_time= now()  where stu_id=#{id} ")
	public Integer updateStudent(String id, String name, String sex, String ingrade, String inclassname, String tele, String idcard);
	
	@Update("delete from student_info where stu_id=#{id} ")
	public Integer deleteStudent(String id);
	
	@Update("delete from user_info where user_id=#{id} ")
	public Integer deleteuser(String id);
	
	@Select("select stu_id from student_info")
	public List<String> getSession();
	
	@Select("select tch_id from class_info where grade=#{grade} and class_num=#{classname} ")
	public List<String> getGradeList(String grade, String classname);

	@Select("select count(*) as count from student_info where (#{grade}='' or grade=#{grade}) and (#{classname}='' or class_num=#{classname}) and (#{id}='' or stu_id=#{id}) and (#{session}='' or left(student_info.stu_id, 4)=#{session})")
	
	public List<Map<String, Object>> getCount( String session, String grade, String classname, String id);
}
